package scripts.pre

import com.netflix.config.DynamicBooleanProperty
import com.netflix.config.DynamicPropertyFactory
import com.netflix.config.DynamicStringProperty
import com.netflix.zuul.ZuulFilter
import com.netflix.zuul.context.RequestContext
import org.slf4j.Logger
import org.slf4j.LoggerFactory

//open-service-gateway:DebugModeSetter:pre
//zuul.DebugModeSetter.pre.disable

public class DebugModeSetter extends ZuulFilter {
    private static final Logger logger = LoggerFactory.getLogger(DebugModeSetter.class);


    static final DynamicBooleanProperty couldSetDebug =
            DynamicPropertyFactory.getInstance().getBooleanProperty("zuul.could.set.debug", true);
    static final DynamicBooleanProperty debugRequest =
            DynamicPropertyFactory.getInstance().getBooleanProperty("zuul.debug.request", false);
    static final DynamicStringProperty debugParameter =
            DynamicPropertyFactory.getInstance().getStringProperty("zuul.debug.parameter", "debug");

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return -1001;
    }

    @Override
    public boolean shouldFilter() {
        if (!couldSetDebug.get()) {
            return false
        }
        if ("true".equals(RequestContext.currentContext.getRequest().getParameter(debugParameter.get()))) return true;
        return debugRequest.get();
    }

    @Override
    public Object run() {
        RequestContext.getCurrentContext().setDebugRequest(true)
        RequestContext.getCurrentContext().setDebugRouting(true)

        return null;
    }
}



